package de.slothsoft.commons.swing.util;

import javax.swing.ComboBoxModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import de.slothsoft.commons.IDisplayable;
import de.slothsoft.commons.swing.JCheckList;
import de.slothsoft.commons.swing.KeyControl;
import de.slothsoft.commons.swing.SlothsoftListCellRenderer;

public final class ControlUtil {

	// TODO: AbstractDialog (possible other too) has it's own implementation

	public static JLabel createLabel(JComponent component, String text, int x, int y) {
		JLabel label = new JLabel(text == null ? "" : text + ":"); //$NON-NLS-1$
		component.add(label, GridBagUtil.labelDefaults(x, y));
		return label;
	}

	public static JLabel createLabel(JComponent component, Icon icon, int x, int y) {
		JLabel label = new JLabel(icon);
		component.add(label, GridBagUtil.labelDefaults(x, y));
		return label;
	}

	public static JButton createButton(JComponent component, String text, int x, int y) {
		JButton button = new JButton(text);
		component.add(button, GridBagUtil.buttonDefaults(x, y));
		return button;
	}

	public static JButton createRightButton(JComponent component, String text, int x, int y) {
		JButton button = new JButton(text);
		component.add(button, GridBagUtil.buttonRightDefaults(x, y));
		return button;
	}

	public static JButton createButton(JComponent component, Icon icon, int x, int y) {
		JButton button = new JButton(icon);
		component.add(button, GridBagUtil.toolBarButtonDefaults(x, y));
		return button;
	}

	public static JTextField createText(JComponent component, int x, int y) {
		JTextField textField = new JTextField();
		component.add(textField, GridBagUtil.textFieldDefaults(x, y));
		return textField;
	}

	public static KeyControl createKeyControl(JComponent component, int x, int y) {
		KeyControl textField = new KeyControl();
		component.add(textField, GridBagUtil.textFieldDefaults(x, y));
		return textField;
	}

	public static JTextField createPassword(JComponent component, int x, int y) {
		JPasswordField textField = new JPasswordField();
		component.add(textField, GridBagUtil.textFieldDefaults(x, y));
		return textField;
	}

	public static JTextArea createTextArea(JComponent component, int x, int y) {
		JTextArea textArea = new JTextArea(3, 20);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		component.add(new JScrollPane(textArea), GridBagUtil.textAreaDefaults(x, y));
		return textArea;
	}

	public static JCheckBox createCheckBox(JComponent component, int x, int y) {
		JCheckBox checkBox = new JCheckBox();
		component.add(checkBox, GridBagUtil.textFieldDefaults(x, y));
		return checkBox;
	}

	public static <T> JComboBox<T> createCombo(JComponent parent, ComboBoxModel<T> model, int x, int y) {
		JComboBox<T> comboBox = new JComboBox<T>();
		comboBox.setModel(model);
		comboBox.setRenderer(new SlothsoftListCellRenderer());
		parent.add(comboBox, GridBagUtil.textFieldDefaults(x, y));
		return comboBox;
	}

	public static <T extends IDisplayable> JCheckList<T> createCheckList(JComponent parent, T[] elements, int x, int y) {
		JCheckList<T> checkList = new JCheckList<T>(elements, false);
		parent.add(new JScrollPane(checkList), GridBagUtil.textAreaDefaults(x, y));
		return checkList;
	}

	private ControlUtil() {
		// I'm the wind! (i.e. invisible)
	}

}
